﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SystemCore;
using DatabaseCore;
using Eaktest;

namespace EakTest
{
    public partial class ChangePassword : Form
    {
        IUserDao userDao = new UserDao();
        tbl_User userDto = null;

        private int UserId { get; set; }

        public ChangePassword()
        {
            InitializeComponent();

            UserId = UserContext.Id;

            BindData();
        }

        private void BindData()
        {
            ClearData();

            userDto = userDao.GetUserById(UserId);
            if (userDto == null)
            {
                MessageDialog.Information("User not found");
                return;
            }

            txtEmail.Text = userDto.Email;
        }

        private void ClearData()
        {
            txtEmail.Text = string.Empty;
            txtOldPassword.Text = string.Empty;
            txtNewPassword.Text = string.Empty;
            txtConfirmPassword.Text = string.Empty;
        }

        #region Event
        private void btnSave_Click(object sender, EventArgs e)
        {
            /* Validate before save */
            string NewPassword = txtNewPassword.Text.Trim();
            if (string.IsNullOrEmpty(NewPassword))
            {
                MessageDialog.Information("Please enter a new password.");
                return;
            }

            if (NewPassword != txtConfirmPassword.Text.Trim())
            {
                MessageDialog.Information("Confirm password is not match.");
                return;
            }

            if (txtOldPassword.Text.Trim() != userDto.Password)
            {
                MessageDialog.Information("Old Password is incorrect.");
                return;
            }

            bool isUpdate = userDao.UpdatePassword(UserId, NewPassword);
            if (isUpdate)
            {
                MessageDialog.Information("Save completed.");
                this.Close();
            }
            else
            {
                MessageDialog.Information("Cannot change password.");
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ClearData();
            this.Close();
        }

        private void txtOldPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                btnSave.PerformClick();
            }
        }

        private void txtNewPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                btnSave.PerformClick();
            }
        }

        private void txtConfirmPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                btnSave.PerformClick();
            }
        }
        #endregion
    }
}
